import os

from google.appengine.ext import db
from google.appengine.ext.webapp import template

from model import Category, Wiki

from tools import manager, wiki_manager

class CategoryManager(manager.Manager):
    """
    """

    def select(self, wiki_key, category_key):
        """
        """
        # can't select -> exit
        if wiki_key == '' or category_key == '':
            return

        # get the wiki
        wiki = Wiki().get(wiki_key)

        # get the category
        category = Category().get(category_key)

        # get other wikis in the old category
        query = Wiki().all()
        query.filter('category =', wiki.category)
        query.filter('position >', wiki.position)

        # update other wiki position
        for w in query:
            old_position = w.position - 1
            w.position = old_position
            w.put()

        # update the category
        wiki.category = category

        # set the default position
        query = Wiki().all()
        query.filter('category = ', wiki.category)
        nb_of_wikis = query.count()
        wiki.position = nb_of_wikis + 1

        # save into db
        wiki.put()

    def new(self):
        """
        """
        # create a new category
        category = Category()

        # default title
        category.title = db.Category(' ')

        # last position
        category_query = Category().all()
        category_query.filter('language =', self.language)
        category.position = category_query.count() + 1

        # set the current language
        category.language = self.language

        # save
        category.put()

        # return the new key
        return category.key()

    def edit(self, wiki_key, category_key):
        """
        """
        # get the menu at first
        wi_manager = wiki_manager.WikiManager(
                self.language.key())
        menu = wi_manager.get_menu()

        #
        wiki = Wiki().get(wiki_key)
        #
        category = Category().get(category_key)

        # set the template for edit
        self.template_values['menu'] = menu
        self.template_values['wiki'] = wiki
        self.template_values['category'] = category

        # find the template
        path = os.path.join(os.path.dirname(__file__),
                            '../templates', 'category', 'edit.html')

        # display the category
        return template.render(path, self.template_values)

    def delete(self, category_key):
        """
        """
        # get the category to delete
        category = Category().get(category_key)

        if not category:
            return

        # flag and key
        update = False

        # count the default category
        if category.title == ' ':
            # get the default categry
            query = Category().all()
            query.filter('language =', self.language)
            query.filter('title =', ' ')

            if query.count() > 1:
                update = True

        else:
            update = True

        # little check to keep the minimum requirement
        if update:
            # get other categories for position update
            query = Category().all()
            query.filter('language =', self.language)
            query.filter('position >', category.position)

            # update other wiki position
            for c in query:
                old_position = c.position - 1
                c.position = old_position
                c.put()

            # get other wikis in the old category
            wiki_query = Wiki().all()
            wiki_query.filter('category =', category)

            # get the default categry
            category_query = Category().all()
            category_query.filter('language =', self.language)
            category_query.filter('title =', ' ')
            default_category = category_query.get()

            # set the defautl category to the wikis
            for wiki in wiki_query:
                wiki.category = default_category
                wiki.position = None
                wiki.put()

            # delete from the db
            category.delete()


    def save(self, category_key, title):
        """
        """
        # retrieve the category object from the db
        category = Category().get(category_key)

        # flag for update
        update = False

        # can't be empty
        title = title.strip()
        if title == '':
            title = ' '

        # must keep one like that
        if category.title == ' ':
            # get the default categry
            query = Category().all()
            query.filter('language =', self.language)
            query.filter('title =', ' ')

            if query.count() > 1:
                update = True

        # no pb
        else:
            update = True

        # little check to keep the minimum requirement
        if update:
            # retrieve the wiki title of the category
            category.title = db.Category(title)

            # save the category title
            category.put()
